package session09;

import ATM.FileOperation;
import session09.compent.FontPanel;
import session09.compent.JButtonPanel;
import session09.compent.PanePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyPanel extends PanePanel {
    private JButtonPanel clearBtn;
    private JButtonPanel exitBtn;
    private JButtonPanel sureBtn;

    private FontPanel old;
    private FontPanel pwd;
    private JPasswordField txt1;
    private JPasswordField txt2;

    public ModifyPanel(ATMFrame frame) {
        super(200, 225, 200);

        this.clearBtn = new JButtonPanel("image/buttonClear.jpg", 50, 550);
        this.clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ModifyPanel.this.txt1.setText("");
                ModifyPanel.this.txt2.setText("");
            }
        });
        this.exitBtn = new JButtonPanel("image/buttonOff.jpg", 442, 600);
        this.exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();
                cardLayout.show(atm, "select");
            }
        });
        this.sureBtn = new JButtonPanel("image/buttonSure.jpg", 832, 550);
        this.sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String oldPwd = new String(ModifyPanel.this.txt1.getPassword());
                String newPwd = new String(ModifyPanel.this.txt2.getPassword());
                if (oldPwd.equals(frame.getLogin().login().getPassword()) && !oldPwd.equals(newPwd)) {
                    frame.getLogin().login().setPassword(newPwd);
                    FileOperation.writeFile(frame.getLogin().getAllUsers());
                    JOptionPane.showMessageDialog(frame,"密码修改成功");
                }
            }
        });
        this.getBackLab().add(this.clearBtn);
        this.getBackLab().add(this.exitBtn);
        this.getBackLab().add(this.sureBtn);

        this.old = new FontPanel(40, 30, 20, 150, 30);
        this.old.setText("请输入旧密码");
        this.pwd = new FontPanel(40, 80, 20, 150, 30);
        this.pwd.setText("请输入新密码");
        this.txt1 = new JPasswordField();
        this.txt2 = new JPasswordField();

        this.getImgLab().add(this.old);
        this.txt1.setBounds(200, 30, 160, 30);
        this.getImgLab().add(this.txt1);
        this.getImgLab().add(this.pwd);
        this.txt2.setBounds(200, 80, 160, 30);
        this.getImgLab().add(this.txt2);
    }
}
